#! /bin/bash


# the function for show help information
function show_help() {
  cat <<help_message
USAGE:
  db3up [SUBCOMMAND]
FLAGS:
  -h  Prints help information
SUBCOMMANDS:
  install  install db3 network
help_message
  return 0
}

function install_db3() {
    mkdir -p ~/.db3/bin
    mkdir -p ~/.db3/data
    mkdir -p ~/.db3/logs
    VERSION=`curl -s https://api.github.com/repos/dbpunk-labs/db3/releases/latest | python3  -c 'import sys, json; print(json.load(sys.stdin)["name"])'`
    echo "install db3 with version ${VERSION} at path ~/.db3"
    if [ ! -f ~/.db3/bin/db3 ]
    then
        if [[ "$OSTYPE" == "linux-gnu"* ]]; then
            echo "download db3 package from github"
            curl -L --max-redirs 10 https://github.com/dbpunk-labs/db3/releases/download/${VERSION}/db3-${VERSION}-linux-x86_64.tar.gz -o /tmp/db3-${VERSION}-linux-x86_64.tar.gz
            tar -zxf /tmp/db3-${VERSION}-linux-x86_64.tar.gz
            cp db3-${VERSION}-linux-x86_64/bin/* ~/.db3/bin
        elif [[ "$OSTYPE" == "darwin"* ]]; then
            echo "download db3 package from github"
            curl -L --max-redirs 10 https://github.com/dbpunk-labs/db3/releases/download/${VERSION}/db3-${VERSION}-macos-x86_64.tar.gz -o /tmp/db3-${VERSION}-macos-x86_64.tar.gz
            tar -zxf /tmp/db3-${VERSION}-macos-x86_64.tar.gz
            cp db3-${VERSION}-macos-x86_64/bin/* ~/.db3/bin
        else
            echo "$OSTYPE is not supported, please give us a issue https://github.com/dbpunk-labs/db3/issues/new/choose"
            exit 1
        fi
        export PATH=~/.db3/bin:$PATH
    fi
    echo "please input the admin address to manage the db3 network"
    read -p 'address: ' adminAddress
    echo $adminAddress > ~/.db3/admin
    echo "nice jobs, you have installed db3 network! and you can do the following actions"
    echo "1. run db3up start to start a onebox environment for development"
}

function start_onebox() {
    killall db3 > /dev/null 2>&1
    export PATH=~/.db3/bin:$PATH
    echo "you can see more log in the following path"
    echo "1. ~/.db3/logs/store.log"
    echo "2. ~/.db3/logs/indexer.log"
    echo "ctrl-c to kill the node"

    trap_ctrlc() {
        killall db3
        echo "kill db3 network"
    }
    trap trap_ctrlc INT
    wait
}

function get_opts() {
  #  Parse options to the main command.
  while getopts ":h" opt; do
    case "${opt}" in
      h)
        #  Display help.
        show_help
        ;;
      \?)
        echo "bad options"
        exit 1
        ;;
    esac
  done
  shift $((OPTIND -1))
  #  Remove the main command from the argument list.
  local -r _subcommand="${1:-}"
  if [[ -z ${_subcommand} ]]; then
    return 0
  fi

  shift
  case "${_subcommand}" in
    join_devnet)
        join_devnet
    ;;
    install)
      install_db3
    ;;
    localnet)
      start_localnet
      ;;
    *)
      #  Unrecognized option, get help.
      echo "Invalid subcommand: ${_subcommand}!"
      show_help
      ;;
  esac
  return 0
}

function main() {
  get_opts "${@}"
  return 0
}

main "${@}"

